package com.heima.controller;

import com.heima.common.Result;
import com.heima.domain.dto.LoginDto;
import com.heima.domain.dto.UserDto;
import com.heima.domain.po.User;
import com.heima.domain.vo.LoginUserVo;
import com.heima.domain.vo.RoleVo;
import com.heima.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user-service")
@Api(tags = "文件上传")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/user/login")
    public LoginUserVo login(@RequestBody LoginDto dto){
       return userService.login(dto);
    }

    @GetMapping("/user/search")
    @ApiOperation("人员分页查询")
    public Result getByPage(@RequestParam(required = false,defaultValue = "1") Integer pageIndex,
                            @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                            @RequestParam(required = false) String userName){
        Result result = userService.getByPage(pageIndex,pageSize,userName);
        return result;
    }

    @GetMapping("/role")
    @ApiOperation("查询角色列表")
    public List<RoleVo> getAllRole(){
        List<RoleVo> list = userService.getAllRole();
        return list;
    }

    @PostMapping("/user")
    @ApiOperation("新增人员")
    public Boolean addUser(@RequestBody UserDto dto){
        Boolean flag = userService.addUser(dto);
        return flag;
    }

    @PutMapping("/user/{userId}")
    @ApiOperation("修改人员")
    public Boolean updateUser(@RequestBody UserDto dto,@PathVariable Integer userId){
        Boolean flag = userService.updateUser(dto,userId);
        return flag;
    }

    @DeleteMapping("/user/{userId}")
    @ApiOperation("删除人员")
    public Boolean deleteUser(@PathVariable Integer userId){
        try {
            userService.lambdaUpdate().eq(userId != null, User::getId,userId).remove();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
